const hre = require("hardhat");

/**
 * 用法:
 *   # 使用 .env 里的 PRIVATE_KEY 对应账户
 *   npx hardhat run scripts/check-balance.js --network xlayerTestnet
 *
 *   # 指定地址(无需私钥)，只读
 *   ADDRESS=0xYourAddress npx hardhat run scripts/check-balance.js --network xlayerTestnet
 */
async function main() {
  const network = hre.network.name;
  let address = process.env.ADDRESS;

  if (!address) {
    // 取第一个 signer
    const signers = await hre.ethers.getSigners();
    if (signers.length === 0) {
      throw new Error("没有可用 signer，请确认 PRIVATE_KEY 已在 .env 中设置");
    }
    address = await signers[0].getAddress();
  }

  const balanceWei = await hre.ethers.provider.getBalance(address);
  console.log("balanceWei",balanceWei)
  const balanceEth = hre.ethers.formatEther(balanceWei);

  console.log("=== Balance Check ===");
  console.log("Network:", network);
  console.log("Address:", address);
  console.log("Balance (wei):", balanceWei.toString());
  console.log("Balance (ETH):", balanceEth);
  console.log("提示: 这是测试网 OKB 计价 (与 ETH 同样 18 位精度)");
}

main().catch(e => { console.error(e); process.exit(1); });
